This function opens a window and displays message as well as a space for the user to input a string (which will contain the supplied default string to start with). This is an asynchronous function, and as such GameMaker Studio 2 does not block the device it is being run on while waiting for answer, but rather keeps on running events as normal. Once the user has typed out their string and pressed the "Okay" button, an asynchronous Dialog event is triggered which, for the duration of that event only, will have a ds_map stored in the variable async_load.
This map will contain the three keys, "id", "status", and "result". "id" is the value that was returned by the function when called, the "status" will be either true for the "Okay" button being pressed, or false if the message was cancelled (where applicable as not all target platforms permit the message to be cancelled). Finally "result" will return the string that the user input (or an empty string "" if none was supplied).
get_string_async(string, default);
Argument | Description |
---|---|
String | The message to show in the dialog. |
default | The default string. |
Real
The left mouse press event (for example) of the object that is showing the message would have the following code:
msg = get_string_async("What's your
name?","Anon");
The above will show a message requesting that the user input a string and press "Okay". The function id is stored in the variable "msg" and will be used in the asynchronous Dialogs event as shown below:
var i_d = ds_map_find_value(async_load, "id");
if i_d == msg
{
if ds_map_find_value(async_load,
"status")
{
if
ds_map_find_value(async_load, "result") != ""
{
global.Name
= ds_map_find_value(async_load, "result");
}
}
}
The above code checks the "id" key of the returned ds_map against the value stored in the variable "msg". If they are the same, it then checks to see if "Okay" was pressed (rather than the window being closed/cancelled) and if it returns true it then checks the "result" of a string to make sure that no empty strings were returned before setting a global variable.